package servlet.order;

import domain.Orders;
import domain.Page;
import service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/GotoOrderServlet")
public class GotoOrderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        if(session.getAttribute("zhi") == null) {
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().print("<script language='javascript'>  \n" +
                    "    alert('您还没有登录，请返回登录界面进行登录！！！');  \n" +
                    "    window.location.href = 'index.jsp';  \n" +
                    "</script>");
        }
        int userid = Integer.parseInt(session.getAttribute("zhi").toString());
        String current = req.getParameter("current");
        int curPage = (current==null?1:Integer.parseInt(current));
        OrderService orderService = new OrderService();
        List<Orders> orders = orderService.findOrderByUserId(userid);
        session.setAttribute("order", orders);
        List<Page> pages = orderService.getPages(userid);
        req.setAttribute("pages", pages);
        req.setAttribute("current", curPage);
        req.setAttribute("total",pages.size());
        req.setAttribute("userid", userid);
        req.getRequestDispatcher("/order.jsp").forward(req, resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
